<template>
  <div class="login-container">
    <div class="login-box">
      <h3 class="title">宜宾后台管理系统</h3>
      <el-form
          class="login-form" ref="form" label-position="left"
          :model="form" :rules="rules" autocomplete="on"
      >
        <el-form-item prop="username">
          <span class="svg-container"><svg-icon icon-class="user"/></span>
          <el-input
              ref="username"
              v-model="form.username"
              placeholder="用户名"
              name="username"
              type="text"
              tabindex="1"
              autocomplete="on"
          />
        </el-form-item>
        <el-form-item prop="password">
          <span class="svg-container"><svg-icon icon-class="password"/></span>
          <el-input
              :key="passwordType"
              ref="password"
              v-model="form.password"
              :type="passwordType"
              placeholder="密码"
              name="password"
              tabindex="2"
              autocomplete="on"
              @keyup.enter.native="onLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'"/>
          </span>
        </el-form-item>
        <el-button class="submit" type="primary" @click="onLogin">登录</el-button>
      </el-form>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'Login',
    data() {
      return {
        form: {
          username: '',// '郑洋',
          password: ''// '123456'
        },
        rules: {
          username: [{ required: true, trigger: 'blur', message: '请输入用户名' }],
          password: [{ required: true, trigger: 'blur', message: '请输入密码' }]
        },
        passwordType: 'password',
        isRemember: false,
        isProtocol: false,
        loading: false
      }
    },
    methods: {
      showPwd() {
        if (this.passwordType === 'password') {
          this.passwordType = ''
        } else {
          this.passwordType = 'password'
        }
        this.$nextTick(() => {
          this.$refs.password.focus()
        })
      },
      onLogin() {
        this.$refs.form.validate(valid => {
          if (valid) {
            this.loading = true
            this.$store.dispatch('user/login', this.form).then(() => {
              this.$router.push({ path: '/' })
              this.loading = true
            }).catch(() => {
              this.loading = false
            })
          }
        })
      }
    },
    mounted() {
      if (this.form.username === '') {
        this.$refs.username.focus()
      } else if (this.form.password === '') {
        this.$refs.password.focus()
      }
    }
  }
</script>

<style lang="scss" scoped>
  .login-container {
    min-height: 100%;
    width: 100%;
    background-color: #2d3a4b;
    overflow: hidden;

    $bg: #283443;
    $light_gray: #fff;
    $cursor: #fff;
    $dark_gray: #889aa4;

    .login-box {
      position: relative;
      width: 420px;
      padding: 30px;
      background-color: #FFF;
      border-radius: 2px;
      margin: 0 auto;
      margin-top: 160px;
      overflow: hidden;

      .title {
        margin-top: 0;
        text-align: center;
      }

      .login-form {
        .el-input {
          display: inline-block;
          height: 47px;
          width: 85%;

          ::v-deep input {
            background: transparent;
            border: 0px;
            -webkit-appearance: none;
            border-radius: 0px;
            padding: 12px 5px 12px 15px;
            height: 100%;

            &:-webkit-autofill {
              box-shadow: 0 0 0px 1000px #fff inset !important;
              -webkit-text-fill-color: #454545 !important;
            }
          }
        }

        .el-form-item {
          border: 1px solid #e7e7eb;
          color: #454545;
          transition: all 0.3s;

          &:hover {
            border: 1px solid rgb(24, 144, 255);
          }
        }

        .svg-container {
          padding: 6px 5px 6px 15px;
          color: $dark_gray;
          vertical-align: middle;
          width: 30px;
          display: inline-block;
        }

        .show-pwd {
          position: absolute;
          right: 10px;
          top: 7px;
          font-size: 16px;
          color: $dark_gray;
          cursor: pointer;
          user-select: none;
        }

        .submit {
          width: 100%;
          height: 45px;
        }
      }

      .text-box {
        padding-top: 10px;

        .row {
          margin-top: 10px;
          font-size: 14px;

          .el-checkbox {
            margin-right: 6px;
          }
        }
      }
    }
  }
</style>
